﻿using System;
using System.Windows.Forms;
namespace YanZhiwei.DotNet2.Utilities.WinForm
{
    /// <summary>
    /// MenuStrip 帮助类
    /// </summary>
    public static class MenuStripHelper
    {
        #region 遍历MenuStrip控件
        /// <summary>
        /// 遍历MenuStrip控件
        /// </summary>
        /// <param name="menu">MenuStrip</param>
        /// <param name="loopMenuRule">遍历的时候动作『委托』</param>
        public static void LoopMenuItem(this MenuStrip menu, Action<ToolStripMenuItem> loopMenuRule)
        {
            foreach (ToolStripMenuItem _menu in menu.Items)
            {
                if (loopMenuRule != null)
                    loopMenuRule(_menu);
                LoopDropDownItems(_menu, loopMenuRule);
            }

        }
        private static void LoopDropDownItems(ToolStripMenuItem _menu, Action<ToolStripMenuItem> loopMenuRule)
        {
            for (int i = 0; i < _menu.DropDownItems.Count; i++)
            {
                if (!(_menu.DropDownItems[i] is ToolStripSeparator))
                {
                    ToolStripMenuItem _dropItemMenu = (ToolStripMenuItem)_menu.DropDownItems[i];
                    if (loopMenuRule != null)
                        loopMenuRule(_dropItemMenu);
                    LoopDropDownItems(_dropItemMenu, loopMenuRule);
                }
            }
        }

        #endregion 

    }
}
